package cn.psh.pet.controller;

import cn.psh.basic.util.exception.BusinessException;
import cn.psh.pet.service.ISearchMasterMsgService;
import cn.psh.pet.domain.SearchMasterMsg;
import cn.psh.pet.query.SearchMasterMsgQuery;
import cn.psh.basic.util.AjaxResult;
import cn.psh.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;


    /**
     * 保存和修改公用的
     * @param searchMasterMsg  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addAndUpdate(@RequestBody SearchMasterMsg searchMasterMsg){
        try {
            if( searchMasterMsg.getId()!=null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.save(searchMasterMsg);
            return AjaxResult.OJBK();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.OJBK().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.del(id);
            return AjaxResult.OJBK();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.OJBK().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public SearchMasterMsg get(@PathVariable("id")Long id)
    {
        return searchMasterMsgService.queryById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<SearchMasterMsg> list(){

        return searchMasterMsgService.queryAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<SearchMasterMsg> json(@RequestBody SearchMasterMsgQuery query)
    {
        return searchMasterMsgService.queryData(query);
    }


    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request){
        try {
            return searchMasterMsgService.publish(searchMasterMsg,request);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙");
        }

    }

    //1.用户查询自己的所有状态的寻主消息
    @PostMapping("/user")
    public PageList<SearchMasterMsg> user(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request) {
        return searchMasterMsgService.user(query,request);
    }

    //2.管理员查询待处理消息：state = 1
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request) {
        return searchMasterMsgService.toHandle(query,request);
    }

    //3.管理员查询寻主池消息：state = 3
    @PostMapping("/pool")
    public PageList<SearchMasterMsg> pool(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request) {
        return searchMasterMsgService.pool(query,request);
    }

    //4.管理员查询已经完成的寻主消息：state = 2
    @PostMapping("/finish")
    public PageList<SearchMasterMsg> finish(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request) {
        return searchMasterMsgService.finish(query,request);
    }
    //4.管理员查询已经完成的寻主消息：state = 2
    @GetMapping("/reject/{id}")
    public AjaxResult reject(@PathVariable("id")Long id) {
        try {
            searchMasterMsgService.reject(id);
            return AjaxResult.OJBK();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.OJBK().setSuccess(false).setMessage("拒单失败！");
        }
    }
    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody Map<String,String> map) {
        try {
            searchMasterMsgService.accept(map);
            return AjaxResult.OJBK();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.OJBK().setSuccess(false).setMessage("拒单失败！");
        }
    }
}
